package com.example.bzh.gamecat.ui.society.allsociety;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.example.bzh.gamecat.R;
import com.example.bzh.gamecat.base.BaseActivity;
import com.example.bzh.gamecat.bean.AllGuildListBean;
import com.example.bzh.gamecat.bean.AllSocietyBean;
import com.handmark.pulltorefresh.library.ILoadingLayout;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

public class AllSocietyActivity extends BaseActivity implements AllSocietyContract.IView {

    @BindView(R.id.tv_title_second)
    TextView tvTitleSecond;
//    @BindView(R.id.lv_all_society)
//    ListView lvAllSociety;
    @BindView(R.id.lv_all_society)
    PullToRefreshListView lvAllSociety;
    private  List<AllGuildListBean> allList = new ArrayList<>();
    private int current = 0;

    private final int ALL_SOCIETY = 0;

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case ALL_SOCIETY:
                    List<AllGuildListBean> list = (List<AllGuildListBean>) msg.obj;
                    Log.i("TAG", "allGuildListBean=========" + list.size());
                    allList.addAll(list);

                    final AllSocietyAdapter adapter = new AllSocietyAdapter(allList,AllSocietyActivity.this);
                    ListView listView = lvAllSociety.getRefreshableView();

                    View view= LayoutInflater.from(AllSocietyActivity.this).inflate(R.layout.head_list_all_society,null);
                    //添加listview头视图
                    if (current==0){

                        listView.addHeaderView(view);
                    }
                    listView.setAdapter(adapter);
                    lvAllSociety.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            lvAllSociety.onRefreshComplete();
                        }
                    }, 1000);
                    lvAllSociety.setMode(PullToRefreshBase.Mode.BOTH);
                    initPullTo();
                    lvAllSociety.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
                        @Override
                        public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                            presenter.initData(initParams());
                            adapter.notifyDataSetChanged();

                        }

                        @Override
                        public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                            current++;
                            presenter.initData(initParams());
                            adapter.notifyDataSetChanged();

                        }
                    });

//                    Intent intent = getIntent();
//                    String type = intent.getStringExtra("type");
//                    if ("play".equals(type)){
//                        current++;
//
//                    }


                    break;
            }
        }


    };


    private AllSocietyContract.IModel model;
    private AllSocietyContract.IPresenter presenter;

    private void initPullTo() {
        ILoadingLayout startLabels = lvAllSociety
                .getLoadingLayoutProxy(true, false);
        startLabels.setPullLabel("下拉刷新...");// 刚下拉时，显示的提示
        startLabels.setRefreshingLabel("正在载入...");// 刷新时
        startLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示

        ILoadingLayout endLabels = lvAllSociety.getLoadingLayoutProxy(
                false, true);
        endLabels.setPullLabel("上拉刷新...");// 刚下拉时，显示的提示
        endLabels.setRefreshingLabel("正在载入...");// 刷新时
        endLabels.setReleaseLabel("放开刷新...");// 下来达到一定距离时，显示的提示
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        model = new AllSocietyModel();
        presenter = new AllSocietyPresenter(this, model);

        Intent intent = getIntent();
        String id = intent.getStringExtra("ID");

        if (id!=null){
            Log.i("TAG","id========="+id);
            presenter.initData(initParams2(id));
        }else{
            Log.i("TAG","noid------------------");

            presenter.initData(initParams());
        }
    }

    @Override
    public void initData() {
        super.initData();

    }

    @Override
    public void initView() {
        super.initView();
    }

    @Override
    public int getLayoutId() {
        return R.layout.activity_all_society;
    }

    private HashMap<String, String> initParams() {
        HashMap<String, String> params = new HashMap<>();
        params.put("apiVersion", "215");
        params.put("channelId", "baidu");
        params.put("devicesId", "867581020760386");
        params.put("terminalName", "8676-A01");
        params.put("terminalType", "2");
        params.put("token", "01a30e837881ad7a17b8fce9def84956");
        params.put("version", "2.1.6");
        params.put("current", String.valueOf(current));
        params.put("version", "10");
        params.put("methodName", "guild_searchGuild");
        return params;
    }
    private HashMap<String, String> initParams2(String id) {
        HashMap<String, String> params = new HashMap<>();
        params.put("apiVersion", "215");
        params.put("channelId", "baidu");
        params.put("devicesId", "867581020760386");
        params.put("terminalName", "8676-A01");
        params.put("terminalType", "2");
        params.put("token", "01a30e837881ad7a17b8fce9def84956");
        params.put("version", "2.1.6");
        params.put("current", String.valueOf(current));
        params.put("version", "10");
        params.put("methodName", "guild_getGuildsByGame");
        params.put("mId", id);

        return params;
    }

    @OnClick({R.id.iv_back})
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.iv_back:
                finish();
                break;
        }
    }

    @Override
    public void OnSeccessAllSociety(AllSocietyBean allSocietyBean) {
        Message allSocietyMessage = handler.obtainMessage(ALL_SOCIETY, allSocietyBean.getData().getGuildList());
        handler.sendMessage(allSocietyMessage);
    }

    @Override
    public void OnFailAllSociety(String str) {
        Toast.makeText(AllSocietyActivity.this, str, Toast.LENGTH_SHORT).show();
    }
}
